﻿using Newtonsoft.Json.Linq;
using Ruici.Web.Services;
using Ruici.Web.Services.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ruici.BLL
{
    public class DocumentBll
    {
        private DocumentService documentService = new DocumentService();

        public List<ArticleResponse> GetArticleList(string channel_name, int category_id, int page_size, int page_index, string keys, out int totalcount)
        {
            List<ArticleResponse> articlemodel = documentService.GetArticleList(channel_name, category_id, page_size, page_index, keys, "",out totalcount);
            return articlemodel;
        }

        public List<ArticleResponse> GetArticleList(string channel_name, List<int> categoryIdList, int page_size, int page_index, string keys, out int totalcount)
        {
            List<ArticleResponse> articleList = new List<ArticleResponse>();
            int totalRecordCount = 0;
            int index = 0;
            int fillRecord = 0;
            foreach (int categoryId in categoryIdList)
            {
                List<ArticleResponse> articlemodel = documentService.GetArticleList(channel_name, categoryId, page_size, page_index, keys,"", out totalcount);

                if (articleList.Count != page_size && page_index <= ((totalcount + page_size - 1) / page_size))
                {
                    if(articleList.Count == 0)
                    {
                        int i = 0;
                        foreach (ArticleResponse article in articlemodel)
                        {
                            i++;
                            if (i <= fillRecord)
                                continue;
                            else
                            {
                                if (articleList.Count < page_size)
                                    articleList.Add(article);
                            }
                        }
                    }                    

                    if (page_index == ((totalcount + page_size - 1) / page_size))
                    {
                        page_index = 1;
                        totalRecordCount = totalRecordCount + totalcount;
                        continue;
                    }
                    else
                    {
                        articlemodel = documentService.GetArticleList(channel_name, categoryId, page_size, ++page_index, keys,"", out totalcount);
                        if (articleList.Count != page_size)
                        {
                            foreach (ArticleResponse article in articlemodel)
                            {
                                if(articleList.Count < page_size)
                                   articleList.Add(article);
                            }
                        }                       
                    }
                    ++page_index;
                }
                
                totalRecordCount = totalRecordCount + totalcount;

                if (page_index >((totalcount + page_size-1)/page_size))
                {
                    fillRecord = ((totalRecordCount + page_size - 1) / page_size) * page_size - totalRecordCount;                    
                    page_index = page_index - ((totalRecordCount + page_size - 1) / page_size);
                }                             
            }
            totalcount = totalRecordCount;
            return articleList;
        }

        public ArticleResponse GetArticleDetial(int keyWord)
        {
            ArticleResponse ArticleInfo = documentService.GetArticleInfo(keyWord);

            return ArticleInfo;
        }

        public Dictionary<string, List<ArticleCategoryResponse>> GetCategoryList(string sChannelName, int iCategoryId)
        {
            Dictionary<string, List<ArticleCategoryResponse>> artCategoryList = documentService.GetArticleCategoryList(sChannelName, iCategoryId);
            return artCategoryList;
        }

        public List<ArticleCategoryResponse> GetCategories(string sChannelName, int iParentId)
        {
            List<ArticleCategoryResponse> CategoryList = documentService.GetCategories(sChannelName, iParentId);
            return CategoryList;
        }
    }
}
